﻿@page "/Rule"
@using WorkFlow.Data
@using WorkFlow.Data.Model
@inject List<WorkFlow.Data.Model.RuleEntity> ruleList
@inject IJSRuntime JSRunTime
@inject CommentTool commentTool
@inject DataManager dataManager
<h1>Rule</h1>

<div>
    RuleName&nbsp;<input type="text" id="Name" @bind="@_Name">
    &nbsp;&nbsp;<button @onclick="@(()=>Search())" class="btn btn-primary btn-xs">Search</button>
    &nbsp;&nbsp;<button class="btn btn-primary btn-xs" data-toggle="modal" data-target="#EditRule">Add Rule</button>
</div>
<br />
<div>
    <table class="table">
        <thead>
            <tr>
                <th>Sno</th>
                <th>ID</th>
                <th style="width: 100%;">Name</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>
            @if (ruleList != null)
            {
                for (var i = 0; i < ruleList.Count; i++)
                {
                    WorkFlow.Data.Model.RuleEntity ruleItem = ruleList[i];
                    <tr>
                        <td>@(i+1)</td>
                        <td>@ruleItem.ID</td>
                        <td><a @onclick="()=>HiddenNav()" href="RuleDetail/@ruleItem.ID" target="_RuleDetail">@ruleItem.Name</a></td>
                        <td><button class="btn btn-outline-primary" @onclick="@(()=>EditRule(@ruleItem.ID,@ruleItem.Name))" data-toggle="modal" data-target="#EditRule">Edit</button></td>
                        <td><button class="btn btn-primary" @onclick="@(()=>DeleteRule(@ruleItem.ID))">Delete</button></td>
                    </tr>
                }
            }
        </tbody>
    </table>
</div>

<div class="modal fade" id="EditRule" tabindex="-1" role="dialog" aria-labelledby="ParamertersLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="title">Rule</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="model-body">
                &ensp;RuleName&nbsp;
                @if (@oprationType == 0)
                {
                    <input type="text" id="Name" @bind="@_EditName" style="width: 100%;">
                }
                else
                {
                    <input type="text" id="Name" @bind="@_EditName" style="width: 100%;">
                }
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">close</button>
                <button type="button" class="btn btn-primary" @onclick="Save">Save</button>
            </div>
        </div>

    </div>
</div>

@code {
    private IJSObjectReference module;
    private int oprationType = 0; //0:Add 1:Edit
    private string _Name;
    private int _EditID;
    private string _EditName;
    protected override async void OnAfterRender(bool firstRender)
    {
        commentTool.ShowNav = true;
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            module = await JSRunTime.InvokeAsync<IJSObjectReference>("import", "/vendor/MyJS.js");
            Search();
        }
    }
    private void Search()
    {
        ruleList = dataManager.GetRule(_Name);
        UpdateUI();
    }
    private void AddRule()
    {
        oprationType = 0;
        _EditName = "";
    }

    private void EditRule(int ID, string editName)
    {
        oprationType = 1;
        _EditID = ID;
        _EditName = editName;
    }

    private void Save()
    {
        string result;
        if (oprationType == 0)
        {
            result = dataManager.AddRule(_EditName);
        }
        else
        {
            result = dataManager.EditRule(_EditID, _EditName);
        }
        if (string.IsNullOrEmpty(result))
        {
            this.module.InvokeVoidAsync("CloseDialog", "close");
            Search();
        }
        else
        {
            this.module.InvokeAsync<bool>("IsContinue", result);
        }
    }


    private async void DeleteRule(int ID)
    {
        bool bo = await this.module.InvokeAsync<bool>("IsContinue", "Are you sure delete this rule?");
        if (bo)
        {
            dataManager.DeleteRule(ID);
            WorkFlow.Data.Model.RuleEntity rule = ruleList.Find(a => a.ID == ID);
            ruleList.Remove(rule);
            UpdateUI();
        }
    }
    private void HiddenNav()
    {
        commentTool.ShowNav = false;
    }
    private async void UpdateUI()
    {
        await InvokeAsync(() => StateHasChanged());
    }
}
